<template>
  <transition name="slide">
    <div class="user-center">
      <div class="back" @click.stop="goBack">
        <i class="iconfont icon-back1"></i>
      </div>
      <div class="switches-wrapper">
        <Switches :switches="switches" :currentIndex="currentIndex" @switch="switchItem"></Switches>
      </div>
      <div class="play-btn" ref="playBtn" @click.stop="playRandom">
        <i class="iconfont icon-play"></i>
        <span class="text">随机播放全部</span>
      </div>
      <div class="list-wrapper" ref="listWrapper">
        <scroll class="list-scroll" :data="favoriteList" v-if="currentIndex === 0" ref="scroll">
          <div class="list-inner">
            <song-list :songs="favoriteList" @toplay="toSelectSong"></song-list>
          </div>
        </scroll>
        <scroll class="list-scroll" :data="playHistory" v-if="currentIndex === 1" ref="scroll">
          <div class="list-inner">
            <song-list :songs="playHistory" @toplay="toSelectSong"></song-list>
          </div>
        </scroll>
      </div>
      <!-- 添加歌曲到列表的提示弹窗 -->
      <top-tip ref="topTip">
        <div class="tip-title">
          <i class="icon-ok"></i>
          <span class="text">1首歌曲已经添加到播放队列</span>
        </div>
      </top-tip>
      <!-- 没有结果的展示 -->
      <div class="no-result-wrapper" v-show="isResult">
        <no-result  :title="noReasultDesc"></no-result>
      </div>
    </div>
  </transition>
</template>
<script>
import Switches from "@/base/switches/switches";
import scroll from "@/base/scroll/scroll";
import songList from "@/base/song-list/song-list";
import { mapGetters, mapActions } from "vuex";
import { songListMixin, playlistMixin } from "@/common/js/mixin";
import topTip from "@/base/top-tip/top-tip";
import Song from "@/common/js/SongObject";
import noResult from "../../base/no-result/no-result"
export default {
  mixins: [songListMixin, playlistMixin],
  data() {
    return {
      switches: [{ name: "我喜欢的" }, { name: "最近听的" }]
      // currentIndex:0,
    };
  },
  computed: {
    ...mapGetters(["playlist", "favoriteList"]),
    isResult(){
      if(this.currentIndex === 0){
        return !this.favoriteList.length
      }else{
        return !this.playHistory.length
      }
    },
    noReasultDesc(){
       if(this.currentIndex === 0){
        return '您暂时还没有收藏歌曲'
      }else{
        return  '最近暂无播放记录'
      }
    }
  },
  components: {
    Switches,
    scroll,
    songList,
    topTip,
    noResult
  },
  methods: {
    //点击返回上一页面
    goBack() {
      this.$router.back();
    },
    // mixin
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? "60px" : "";
      this.$refs.listWrapper.style.bottom = bottom;
      this.$refs.scroll.refresh();
    },
    //点击随机播放按钮，播放音乐
    playRandom() {
      let list = this.currentIndex === 0 ? this.favoriteList : this.playHistory
      if(list.length === 0){
        return
      }
      list = list.map((item) => {
        return new Song(item)
      })
      this.randomPlay({
        list: list
      });
    },
    ...mapActions(['randomPlay'])
  }
};
</script>
<style lang="stylus">
@import '../../common/stylus/variable';

.user-center {
  position: fixed;
  top: 0;
  bottom: 0;
  z-index: 100;
  width: 100%;
  background: $color-background;

  &.slide-enter-active, &.slide-leave-active {
    transition: all 0.3s;
  }

  &.slide-enter, &.slide-leave-to {
    transform: translate3d(100%, 0, 0);
  }

  .back {
    position: absolute;
    top: 0;
    left: 6px;
    z-index: 50;

    .icon-back1 {
      display: block;
      padding: 10px;
      font-size: $font-size-large-x;
      color: $color-theme;
    }
  }

  .switches-wrapper {
    margin: 10px 0 30px 0;
  }

  .play-btn {
    box-sizing: border-box;
    width: 135px;
    padding: 7px 0;
    margin: 0 auto;
    text-align: center;
    border: 1px solid $color-text-l;
    color: $color-text-l;
    border-radius: 100px;
    font-size: 0;

    .icon-play {
      display: inline-block;
      vertical-align: middle;
      margin-right: 6px;
      font-size: $font-size-medium-x;
    }

    .text {
      display: inline-block;
      vertical-align: middle;
      font-size: $font-size-small;
    }
  }

  .list-wrapper {
    position: absolute;
    top: 110px;
    bottom: 0;
    width: 100%;

    .list-scroll {
      height: 100%;
      overflow: hidden;

      .list-inner {
        padding: 20px 30px;
      }
    }
  }

  .no-result-wrapper {
    position: absolute;
    width: 100%;
    top: 50%;
    transform: translateY(-50%);
  }
}
</style>